﻿using System;
using System.Data;
using DataGeneration.Domain.Helpers;
using DataGeneration.Domain.Logging;

namespace DataGeneration.Domain.DataAccessLayer
{
    /// <summary>
    /// Defines the class DatabaseHelper
    /// </summary>
    public class DatabaseHelper
    {
        public static string IndentityInsertOn(string tableName, bool identityInsert)//,XElement entityNode)
        {
            if (identityInsert && ParameterizedAppSettings.DatabaseType.Equals(SupportedDatabase.MSSqlServer))
            {
                return string.Format("SET IDENTITY_INSERT {0} ON;", tableName);
            }
            return "";
        }
        public static string IndentityInsertOff(string tableName, bool identityInsert)//, XElement entityNode)
        {
            if (identityInsert && ParameterizedAppSettings.DatabaseType.Equals(SupportedDatabase.MSSqlServer))
            {
                return string.Format("; SET IDENTITY_INSERT {0} OFF;", tableName);
            }
            return "";
        }

        /// <summary>
        /// Gets the last entity id from db.
        /// </summary>
        /// <param name="tableName">Name of the table.</param>
        /// <param name="columnName">Name of the column.</param>
        /// <param name="dataAccess">The data access.</param>
        /// <returns></returns>
        public static int GetLastEntityIdFromDb(string tableName, string columnName, DataAccess dataAccess)
        {
            int lastId;
            try
            {
                dataAccess.InitializeCommand(string.Format("SELECT  MAX(CAST({0} AS INTEGER)) FROM {1}", columnName, tableName), CommandType.Text);
                lastId = dataAccess.ExecuteScalar<int>();
            }
            catch (Exception exception)
            {
                Logger.WriteMessage(exception.Message);
                throw;
            }

            return lastId;
        }

        /// <summary>
        /// Determines whether [is value unique in database] [the specified table name].
        /// </summary>
        /// <param name="tableName">Name of the table.</param>
        /// <param name="columnName">Name of the column.</param>
        /// <param name="columnValue">The column value.</param>
        /// <param name="dataAccess">The data access.</param>
        /// <returns></returns>
        public static bool IsValueInDatabase(string tableName, string columnName,string columnValue, DataAccess dataAccess)
        {
            int columnCount;
            try
            {
                dataAccess.InitializeCommand(string.Format("SELECT  COUNT(*) FROM {0} WHERE {1} ='{2}'", tableName
                                                                                                       , columnName
                                                                                                       , columnValue)
                                             , CommandType.Text);
                columnCount = dataAccess.ExecuteScalar<int>();
            }
            catch (Exception exception)
            {
                Logger.WriteMessage(exception.Message);
                throw;
            }

            return columnCount>0;
        }
    }
}
